<?php


namespace App\Logic;
use App\Models\Login;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class LoginLogic
{

    public static function dologin($data,$user)
    {
        //图片验证码输入错误
        if (!captcha_check($data['code'])) {
            return Response()->json(['code' => 10001, 'message' => '验证码有误']);
        }
        //用户不存在
        if (empty($user)) {
            return Response()->json(['code' => 10002, 'message' => '该用户不存在,请重新输入']);
        }
        //判断手机号是否一致
        if ($data['phone'] !== $user['phone']) {
            return Response()->json(['code' => 10003, 'message' => '手机号输入错误请重新输入']);
        }
        //获取用户密码
        $password = $data['password'];
        //判断密码是否一致
        if (md5($password) !== $user['password']) {
            return Response()->json(['code' => 10004, 'message' => '密码输入错误请重新输入']);
        }
        //判断用户所属的角色名称是否为“超级管理员”
        if ($user['role']['role_name'] == '超级管理员') {
            //session设置为 true
            session(['authUser' => true]);
        } else {
            //定义一个空数组
            $node = [];
            //遍历角色的节点
            foreach ($user['role']['role_node'] as $v) {
                //将每个节点的 ID 添加到数组中
                $node[] = $v['id'];
            }
            //session设置为数组
            session(['authUser' => $node]);
        }

    }
}
